package com.tianqu.tms.www.transfer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransMarketCode;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.controller.PTransactionsInputResultController;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

public class PTransactionsInputResultAction extends TmsAction {
	private Logger logger = Logger
			.getLogger(PTransactionsInputResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		// 重复提交 	
		isToken(mapping, request);
		
		TransferRgActionForm transferRgActionForm = (TransferRgActionForm)uc.getInputForm();
		//
		PTransactionsInputResultController controller = new PTransactionsInputResultController();
		controller.execute(uc);
		
		//
		TransferRgInputViewHelper view = (TransferRgInputViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
		PTransactionsInputResultController.toViewHelper((TransferRgActionForm)form, view);
		Codes code = Codes.getInstance();

		if("2".equals(view.getCarType())||"10".equals(view.getCarType()))
		{
			if(StringUtil.isNumber(view.getCarType())) view.setCarTypeName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, Integer.parseInt(view.getCarType())));
		}else{
			if(StringUtil.isNumber(view.getCarType())) view.setCarTypeName(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE, Integer.parseInt(view.getCarType())));
		}
		
		
		
		view.setOldCardNoId(transferRgActionForm.getIdentification());
		view.setNewCardNoId(transferRgActionForm.getNewIdentification());
		view.setKpDate(DateUtil.getCurrentTimeStr());
		String price = view.getPrice();
		int pp = 0;
		String miw = "";
		String maw = "";
		if (price != null && !"".equals(price)) {
			pp = Integer.parseInt(StringUtil.removeDH(price));
			// 开始对pp进行处理
			miw = pp + ".00";
			maw = cleanZero(splitNum(roundString(String.valueOf(pp)))) + "整";
		}
		view.setMaw(maw);
		view.setMiw(miw);
		TransMarketCode market = new TransMarketCode();
		market = market.getTransMarketCodeInfo(transferRgActionForm.getTransMarket());
		if (market != null) {
			view.setMarketName(market.getMarketName());
			view.setTaxpayerNo(market.getTaxpayerNo());
			view.setAddress(market.getAddress());
			view.setBank(market.getBank());
			view.setMarketPhone(market.getPhone());
		} else {
			view.setMarketName("");
			view.setTaxpayerNo("");
			view.setAddress("");
			view.setBank("");
			view.setMarketPhone("");
		}
		TmsUserProfile profile = uc.getUserProfile();
		view.setUserName(profile.getRealName());
		view.setPrintBsflag("1");
		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
	private String splitNum(String s) {
		if ("".equals(s)) {
			return "";
		}
		int index = s.indexOf(".");
		String intOnly = s.substring(0, index);
		String part1 = this.numFormat(1, intOnly);
		String smallOnly = s.substring(index + 1);
		String part2 = this.numFormat(2, smallOnly);
		String newS = part1 + part2;
		return newS;
	}

	private String roundString(String s) {
		if ("".equals(s)) {
			return "";
		}
		double d = Double.parseDouble(s);
		d = (d * 100 + 0.5) / 100;
		s = new java.text.DecimalFormat("##0.000").format(d);
		int index = s.indexOf(".");
		String intOnly = s.substring(0, index);
		if (intOnly.length() > 13) {
			System.out.println("输入数据过大！（整数部分最多13位！）");
			return "";
		}
		String smallOnly = s.substring(index + 1);
		if (smallOnly.length() > 2) {
			String roundSmall = smallOnly.substring(0, 2);
			s = intOnly + "." + roundSmall;
		}
		return s;
	}

	public static String numFormat(int flag, String s) {
		int sLength = s.length();
		String bigLetter[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
		String unit[] = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾",
				"佰", "仟", "万" };
		String small[] = { "分", "角" };
		String newS = "";
		for (int i = 0; i < sLength; i++) {
			if (flag == 1) {
				newS = newS + bigLetter[s.charAt(i) - 48]
						+ unit[sLength - i - 1];
			} else if (flag == 2) {
				newS = newS + bigLetter[s.charAt(i) - 48]
						+ small[sLength - i - 1];
			}
		}
		return newS;
	}

	private String cleanZero(String s) {
		if ("".equals(s)) {
			return "";
		}
		while (s.charAt(0) == '零') {
			s = s.substring(2);
			if (s.length() == 0) {
				return "零";
			}
		}
		String regex1[] = { "零仟", "零佰", "零拾" };
		String regex2[] = { "零亿", "零万", "零元" };
		String regex3[] = { "亿", "万", "元" };
		String regex4[] = { "零角", "零分" };
		for (int i = 0; i < 3; i++) {
			s = s.replaceAll(regex1[i], "零");
		}
		for (int i = 0; i < 3; i++) {
			s = s.replaceAll("零零零", "零");
			s = s.replaceAll("零零", "零");
			s = s.replaceAll(regex2[i], regex3[i]);
		}
		for (int i = 0; i < 2; i++) {
			s = s.replaceAll(regex4[i], "");
		}
		s = s.replaceAll("亿万", "亿");
		return s;
	}
}
